package com.example.robootttsdemo

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.robootttsdemo.databinding.ActivityRobootBinding

/**
 * 首页
 *
 *
 *
 */
class RoBootActivity : AppCompatActivity() {
    private val multipleRoobItemList: MutableList<MultipleRoobItem> = mutableListOf()
    private lateinit var robootAdapter: RoBootAdapter;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val binding = ActivityRobootBinding.inflate(layoutInflater)
        setContentView(binding.root)

        SpeechUtils.getInstance(this)
        for (index in 0..10) {
            var roobItem: MultipleRoobItem;
            if (index % 2 == 0) {
                roobItem =
                    MultipleRoobItem(
                        "我是一条信息" + (index + 1),
                        "我是头像", MultipleRoobItem.LEFT_ITEM
                    )
            } else {
                roobItem =
                    MultipleRoobItem(
                        "我是一条信息" + (index + 1),
                        "我是头像", MultipleRoobItem.RIGHT_ITEM
                    )
            }
            multipleRoobItemList.add(roobItem)
        }

        robootAdapter = RoBootAdapter(multipleRoobItemList)
        binding.rvMsglist.layoutManager = LinearLayoutManager(this)
        binding.rvMsglist.adapter = robootAdapter
        robootAdapter.addChildClickViewIds(R.id.tv_left_msg, R.id.tv_right_msg);
        robootAdapter.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                R.id.tv_left_msg, R.id.tv_right_msg -> SpeechUtils.getInstance(this)
                    ?.speakText(" " + multipleRoobItemList[position].msg)
            }
        }

        binding.btnSend.setOnClickListener {
            var msg = "我是一条信息";
            if (!binding.edtMsg.text.toString().isEmpty()) {
                msg = binding.edtMsg.text.toString()
            }
            val type: Int;
            if (multipleRoobItemList.size % 2 == 0) {
                type = MultipleRoobItem.LEFT_ITEM
            } else {
                type = MultipleRoobItem.RIGHT_ITEM;
            }
            addItem(
                MultipleRoobItem(
                    msg + (multipleRoobItemList.size + 1),
                    "我是头像", type
                )
            )
        }
    }

    fun addItem(roobItem: MultipleRoobItem) {
        multipleRoobItemList.add(roobItem)
        robootAdapter.notifyDataSetChanged()
    }
}